/*
 * @Author: 小花
 * @Date: 2022-05-21 19:18:11
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-27 23:18:36
 * @FilePath: /permission_client/src/model/LoginView.ts
 * @Description:
 */

import { IMenu } from './MenuView'

export interface ILoginForm {
  account: string
  password: string
}

export interface IResultMap {
  satoken: string,
  routes: IMenu[]
}

/**
 * @description: 菜单处理器
 * @param {IMenu} children 带parentId的子菜单
 * @param {IMenu} parent 无parentId的菜单
 * @return {*}
 */
export const handlerMenu = (children: IMenu[], parent: IMenu[]) => {
  for (const iterator of children) {
    const find = queryParentMenu(iterator.parentId, parent)
    if (find) {
      if (find.children) {
        find.children.push(iterator)
      } else {
        find.children = [] as IMenu[]
        find.children.push(iterator)
      }
    }
  }
}

/**
 * @description: 查询指定菜单的上级菜单
 * @param {*} parentId  需要查询的值
 * @param {*} targar 需要去查询的数组
 * @return {*} 返回当前查询对象
 */
export const queryParentMenu = (parentId: string, targar: IMenu[]) => {
  for (const iterator of targar) {
    if (iterator.id === parentId) {
      return iterator
    } else if (iterator.children && iterator.children.length) {
      const find = queryParentMenu(parentId, iterator.children) as IMenu | undefined
      if (find) return find
    }
  }
}

export default class LoginView {
  form = {} as ILoginForm
  resultMap = {} as IResultMap
}
